//
//  CompanyViewController.m
//  AddressBook
//
//  Created by mac on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "CompanyViewController.h"
#import "SearchCompanyViewController.h"
#import "DepartmentTableViewController.h"

@interface CompanyViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UILabel *network;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray <CompanyModel *> *modelArr;

@end

@implementation CompanyViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reachability];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"query"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(searchCompany) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBar;
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [rightBtn setImage:[UIImage imageNamed:@"btn_ref"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(refreshTheController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBar;
    
    [self.view addSubview:self.tableView];
    
    // 获取本地公司model
    self.modelArr = [UserDataModel getLocalModel].data;
}

-(void)searchCompany{
    self.hidesBottomBarWhenPushed = YES;
    SearchCompanyViewController *search = [[SearchCompanyViewController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

-(void)refreshTheController{
    [self showHudInView:self.view hint:@"刷新中"];
    [UIView animateWithDuration:5.0 animations:^{
        [self hideHud];
        [self showHint:@"刷新成功"];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *companyCell = @"companyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:companyCell];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:companyCell];
    }
    
    // 公司名字
    cell.textLabel.text = self.modelArr[indexPath.row].com_name;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed = YES;
    DepartmentTableViewController *departmentVC = [[DepartmentTableViewController alloc]init];
    // 赋值该公司的部门model
    departmentVC.modelArr = self.modelArr[indexPath.row].info;
    [self.navigationController pushViewController:departmentVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}


- (void)reachability
{
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                [self.network removeFromSuperview];
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                [self.view addSubview:self.network];
                self.tableView.frame = CGRectMake(0, 44, ScreenW, ScreenH - 44);
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                [self.network removeFromSuperview];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                [self.network removeFromSuperview];
                break;
        }
    }];
    // 3.开始监控
    [mgr startMonitoring];
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}


-(UILabel *)network{
    if (!_network) {
        _network = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 40)];
        _network.backgroundColor = [UIColor colorWithRed:230/255.0 green:79.0/255.0 blue:71.0/255.0 alpha:0.9];
        _network.textAlignment = NSTextAlignmentCenter;
        _network.text = @"没有网络";
    }
    return _network;
}


@end
